﻿@using System.IO
@using System.Web.Mvc
@using EasyWeb
@using EasyWeb.Design

@**************************************************************************************************************************************************@
@******************************************************************** Dialog **********************************************************************@
@**************************************************************************************************************************************************@

@helper ConfirmDialog(string text, string action)
{
    <text>EasyWeb.EWHelpers.confirmDialog("@text", @action, "@EWWebResources.Window_Title_Confirm", "@EWWebResources.Button_Ok", "@EWWebResources.Button_Cancel", "@EWWebResources.Button_Close");</text>
}

@helper ConfirmDialog(string text, Func<dynamic, object> action)
{
    <text>EasyWeb.EWHelpers.confirmDialog("@text", @GetHtmlString(action), "@EWWebResources.Window_Title_Confirm", "@EWWebResources.Button_Ok", "@EWWebResources.Button_Cancel", "@EWWebResources.Button_Close");</text>
}

@helper ConfirmDialog(Func<dynamic, object> text, Func<dynamic, object> action)
{
    <text>EasyWeb.EWHelpers.confirmDialog(@GetHtmlString(text), @GetHtmlString(action), "@EWWebResources.Window_Title_Confirm", "@EWWebResources.Button_Ok", "@EWWebResources.Button_Cancel", "@EWWebResources.Button_Close");</text>
}

@*------------------------------------------------------------------------------------------------------------------------------------------------*@

@helper ConfirmDialogAsync(string text, string action, string success, string error)
{
    <text>EasyWeb.EWHelpers.confirmDialogAsync("@text", @action, @success, @error, "@EWWebResources.Window_Title_Confirm", "@EWWebResources.Button_Ok", "@EWWebResources.Button_Cancel", "@EWWebResources.Button_Close");</text>
}

@helper ConfirmDialogAsync(string text, Func<dynamic, object> action, Func<dynamic, object> success, Func<dynamic, object> error)
{
    <text>EasyWeb.EWHelpers.confirmDialogAsync("@text", @GetHtmlString(action), @GetHtmlString(success), @GetHtmlString(error), "@EWWebResources.Window_Title_Confirm", "@EWWebResources.Button_Ok", "@EWWebResources.Button_Cancel", "@EWWebResources.Button_Close");</text>
}

@helper ConfirmDialogAsync(Func<dynamic, object> text, Func<dynamic, object> action, Func<dynamic, object> success, Func<dynamic, object> error)
{
    <text>EasyWeb.EWHelpers.confirmDialogAsync(@GetHtmlString(text), @GetHtmlString(action), @GetHtmlString(success), @GetHtmlString(error), "@EWWebResources.Window_Title_Confirm", "@EWWebResources.Button_Ok", "@EWWebResources.Button_Cancel", "@EWWebResources.Button_Close");</text>
}

@*------------------------------------------------------------------------------------------------------------------------------------------------*@

@helper MessageDialog(string text)
{
    <text>EasyWeb.EWHelpers.messageDialog("@text", "@EWWebResources.Window_Title_Message", "@EWWebResources.Button_Ok", "@EWWebResources.Button_Close");</text>
}

@helper MessageDialog(Func<dynamic, object> text)
{
    <text>EasyWeb.EWHelpers.messageDialog(@GetHtmlString(text), "@EWWebResources.Window_Title_Message", "@EWWebResources.Button_Ok", "@EWWebResources.Button_Close");</text>
}

@*------------------------------------------------------------------------------------------------------------------------------------------------*@

@helper ModalDialog(string title, string html, string action, int? width = null, int? height = null)
{
    <text>EasyWeb.EWHelpers.modalDialog("@title", @html, @action, "@EWWebResources.Button_Ok", "@EWWebResources.Button_Cancel", "@EWWebResources.Button_Close", @GetExpression(width), @GetExpression(height));</text>
}

@helper ModalDialog(string title, Func<dynamic, object> html, Func<dynamic, object> action, int? width = null, int? height = null)
{
    <text>EasyWeb.EWHelpers.modalDialog("@title", @GetHtmlString(html), @GetHtmlString(action), "@EWWebResources.Button_Ok", "@EWWebResources.Button_Cancel", "@EWWebResources.Button_Close", @GetExpression(width), @GetExpression(height));</text>
}

@helper ModalDialog(Func<dynamic, object> title, Func<dynamic, object> html, Func<dynamic, object> action, int? width = null, int? height = null)
{
    <text>EasyWeb.EWHelpers.modalDialog(@GetHtmlString(title), @GetHtmlString(html), @GetHtmlString(action), "@EWWebResources.Button_Ok", "@EWWebResources.Button_Cancel", "@EWWebResources.Button_Close", @GetExpression(width), @GetExpression(height));</text>
}

@*------------------------------------------------------------------------------------------------------------------------------------------------*@

@helper ModalDialogAsync(string title, string html, string action, string success, string error, int? width = null, int? height = null)
{
    <text>EasyWeb.EWHelpers.modalDialogAsync("@title", @html, @action, @success, @error, "@EWWebResources.Button_Ok", "@EWWebResources.Button_Cancel", "@EWWebResources.Button_Close", @GetExpression(width), @GetExpression(height));</text>
}

@helper ModalDialogAsync(string title, Func<dynamic, object> html, Func<dynamic, object> action, Func<dynamic, object> success, Func<dynamic, object> error, int? width = null, int? height = null)
{
    <text>EasyWeb.EWHelpers.modalDialogAsync("@title", @GetHtmlString(html), @GetHtmlString(action), @GetHtmlString(success), @GetHtmlString(error), "@EWWebResources.Button_Ok", "@EWWebResources.Button_Cancel", "@EWWebResources.Button_Close", @GetExpression(width), @GetExpression(height));</text>
}

@helper ModalDialogAsync(Func<dynamic, object> title, Func<dynamic, object> html, Func<dynamic, object> action, Func<dynamic, object> success, Func<dynamic, object> error, int? width = null, int? height = null)
{
    <text>EasyWeb.EWHelpers.modalDialogAsync(@GetHtmlString(title), @GetHtmlString(html), @GetHtmlString(action), @GetHtmlString(success), @GetHtmlString(error), "@EWWebResources.Button_Ok", "@EWWebResources.Button_Cancel", "@EWWebResources.Button_Close", @GetExpression(width), @GetExpression(height));</text>
}

@**************************************************************************************************************************************************@
@***************************************************************** Functions **********************************************************************@
@**************************************************************************************************************************************************@

@functions {
    private static IHtmlString GetHtmlString(Func<dynamic, object> action)
    {
        if (action != null)
        {
            var output = new StringWriter();
            using (var writer = new HtmlTextWriter(output))
            {
                var result = action(null);
                var helperResult = result as HelperResult;
                if (helperResult != null)
                {
                    helperResult.WriteTo(writer);
                }
                else if (result != null)
                {
                    writer.Write(result.ToString());
                }

                return MvcHtmlString.Create(output.ToString());
            }
        }
        return MvcHtmlString.Empty;
    }
    
    private static IHtmlString GetExpression(object value)
    {
        if (value == null)
        {
            return MvcHtmlString.Create("null");
        }
        return MvcHtmlString.Create(value.ToString());
    }
}